package com.ddwl.goods.service;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.exception.CommonException;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.goods.dao.AttributeKeyDao;
import com.ddwl.goods.dao.AttributeValueDao;
import com.ddwl.goods.dao.model.CategoryAttributeKey;
import com.ddwl.goods.dao.model.CategoryAttributeValue;
import com.ddwl.schema.bo.goods.AttributeBo;
import com.ddwl.schema.vo.goods.AttributeValueVo;
import com.ddwl.schema.vo.goods.AttributeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AttributeService {


    @Autowired
    private AttributeKeyDao attributeKeyDao;

    @Autowired
    private AttributeValueDao attributeValueDao;

    /**
     * 获取属性
     *
     * @param attributeBo
     * @return
     */
    public List<AttributeVo> getAttribute(AttributeBo attributeBo) {
        CategoryAttributeKey attributeKey = new CategoryAttributeKey();
        attributeKey.setCategoryId(attributeBo.getCategoryId());
        //获取类目下的属性key
        List<CategoryAttributeKey> categoryAttributeKeyList = attributeKeyDao.select(attributeKey);
        if(categoryAttributeKeyList.isEmpty()){
            return new ArrayList<>();
        }
        CategoryAttributeValue categoryAttributeValue = new CategoryAttributeValue();
        List<AttributeVo> attributeVoList = categoryAttributeKeyList.stream().map(key -> {
            AttributeVo attributeVo = new AttributeVo();
            categoryAttributeValue.setAttributeId(key.getId());
            //根据key值获取对应的value集合
            List<CategoryAttributeValue> categoryAttributeValues = attributeValueDao.select(categoryAttributeValue);
            attributeVo.setAttributeName(key.getAttributeName());
            attributeVo.setId(key.getId());
            attributeVo.setImageNeed(key.getImageNeed());
            attributeVo.setNameSort(key.getNameSort());
            List<AttributeValueVo> valueVos = categoryAttributeValues.stream().map(value -> {
                AttributeValueVo attributeValueVo = new AttributeValueVo();
                BeanUtils.copyProperties(value, attributeValueVo);
                return attributeValueVo;
            }).collect(Collectors.toList());
            attributeVo.setAttributeValues(valueVos);
            return attributeVo;
        }).collect(Collectors.toList());
        return attributeVoList;
    }

    /**
     * 创建或者更新
     * @param attributeBo
     * @return
     */
    public String create(AttributeBo attributeBo){
            CategoryAttributeKey categoryAttributeKey = new CategoryAttributeKey();
            categoryAttributeKey.setCategoryId(attributeBo.getCategoryId());
            //属性keyid如果有则更新，无则新增
        if (attributeBo.getId() != null) {
            categoryAttributeKey.setId(attributeBo.getId());
            categoryAttributeKey.setAttributeName(attributeBo.getAttributeName());
            categoryAttributeKey.setImageNeed(attributeBo.getImageNeed());
            categoryAttributeKey.setNameSort(attributeBo.getNameSort());
            attributeKeyDao.updateByPrimaryKeySelective(categoryAttributeKey);
            if (attributeBo.getAttributeValues() != null) {
            attributeValueDao.deleteByAttributeId(attributeBo.getId());
                for (String value : attributeBo.getAttributeValues()) {
                    CategoryAttributeValue categoryAttributeValue = new CategoryAttributeValue();
                    categoryAttributeValue.setAttributeId(categoryAttributeKey.getId());
                    categoryAttributeValue.setId(UUIDGenerator.getShortUUID());
                    categoryAttributeValue.setAttributeValue(value);
                    attributeValueDao.insert(categoryAttributeValue);
                }
            }
        } else {
            categoryAttributeKey.setId(UUIDGenerator.getShortUUID());
            categoryAttributeKey.setAttributeName(attributeBo.getAttributeName());
            categoryAttributeKey.setImageNeed(attributeBo.getImageNeed());
            categoryAttributeKey.setNameSort(attributeBo.getNameSort());
            attributeKeyDao.insert(categoryAttributeKey);
            if (attributeBo.getAttributeValues() != null) {
                for (String value : attributeBo.getAttributeValues()) {
                    CategoryAttributeValue categoryAttributeValue = new CategoryAttributeValue();
                    categoryAttributeValue.setAttributeId(categoryAttributeKey.getId());
                    categoryAttributeValue.setId(UUIDGenerator.getShortUUID());
                    categoryAttributeValue.setAttributeValue(value);
                    attributeValueDao.insert(categoryAttributeValue);
                }
            }
        }
        return attributeBo.getId();
    }
    public String delete(String id){
        attributeKeyDao.deleteById(id);
        attributeValueDao.deleteByAttributeId(id);
        return id+ Constant.HAVE_DELETED;
    }
}
